package com.sanqing.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanqing.bean.Message;
import com.sanqing.bean.Reply;
import com.sanqing.dao.MessageDAO;
import com.sanqing.dao.ReplyDAO;
import com.sanqing.factory.MessageDAOFactory;
import com.sanqing.factory.ReplyDAOFactory;
import com.sanqing.util.Page;
import com.sanqing.util.PageUtil;

/**
 * Servlet implementation class GetMessage
 */
@WebServlet("/GetMessage")
public class GetMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMessage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("utf-8");//设置编码
		int currentPage=0;
		//获取msgList.jsp中传来的数据
		String currentPageStr=request.getParameter("currentPage");
		int messageID=Integer.parseInt(request.getParameter("messageID"));
		
		if(currentPageStr==null||"".equals(currentPageStr))
		{
			currentPage=1;
		}else{
			currentPage=Integer.parseInt(currentPageStr);
		}
		//数据库查询回复操作
		ReplyDAO replyDAO=ReplyDAOFactory.getReplyDAOInstance();
		Page page=PageUtil.createPage(5, replyDAO.findCountByMsgID(messageID), currentPage);
		List<Reply> replys=replyDAO.findReplyByMsgID(messageID, page);
		
		//将查询结果传到jsp去
		request.setAttribute("replyList", replys);
		request.setAttribute("page", page);
	
		//查询消息
		MessageDAO messageDAO=MessageDAOFactory.getMessageDAOInstance();
		Message message=messageDAO.findMessageById(messageID);
		
		request.setAttribute("message", message);
		ServletContext servletContext=getServletContext();
		RequestDispatcher dispatcher=servletContext.getRequestDispatcher("/View/showMsg.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
